// this file contains event handlers for sub items in CANVAS element
function events()
{
	this.mouseX;
	this.mouseY;
	this.lchar; //last pressed keyboard char
	this.lccode; //last pressed char code
	this.screen;
	this.ltype;
	//main initializer method
	this.init = function()
	{	
		this.baseListener("keypress",document.getElementById("base"),this.handle);
		this.baseListener("mouseout",document.getElementById("base"),this.handle);
		this.baseListener("click",document.getElementById("base"),new Function("globalScreen.focusedElement = \"NONE\"; "));
		this.baseListener("click",document.getElementById("base"),this.handle);
		this.baseListener("mousemove",document.getElementById("base"),this.handle);
		this.baseListener("mousedown",document.getElementById("base"),this.handle);
		this.baseListener("mouseup",document.getElementById("base"),this.handle);
		this.baseListener("mouseover",document.getElementById("base"),this.handle);


	}
	
	//the base listener function
	this.baseListener = function(evnt, elem, func)
	{
    if (elem.addEventListener)  // W3C DOM
        elem.addEventListener(evnt,func,false);
    else if (elem.attachEvent) { // IE DOM
         var r = elem.attachEvent("on"+evnt, func);
	return r;
    }
    else alert('I\'m sorry Dave, I\'m afraid I can\'t do that.');
	}
	
	//handles the standart evens on base
	this.handle = function(e)
	{
		if(e.type == "mousedown")
		{
			dragstart = true;
		}
		else if(e.type == "mouseup" || e.type == "mouseout")
		{
			dragstart = false;
			dragItem = "NONE";
			return false;
		}
		else if(e.type == "mousemove" ) //drag implementation
		{
			if(dragstart && dragItem != "NONE") //we execute drags here
			{
				var mx = globalEventObject.mouseX; var my = globalEventObject.mouseY; ;
				var newX = mx-Math.floor(dragItem.size.width/2);
				var newY = my - Math.floor(dragItem.size.height/2);
				if(newX < dragItem.dragMin[0]) newX = dragItem.dragMin[0]; 
				if( newY < dragItem.dragMin[1]) newY = dragItem.dragMin[1];;
				if(newX > dragItem.dragMax[0]) newX = dragItem.dragMax[0] 
				if(newY > dragItem.dragMax[1]) newY = dragItem.dragMax[1]
				dragItem.position.x = newX; 
				dragItem.position.y = newY;
				globalScreen.updateItemPosition(dragItem);
			}
		}
		
		
		
		
		if( e.type == "keypress" )
		{
			globalEventObject.lchar = String.fromCharCode(e.charCode);
			globalEventObject.lccode = e.charCode;
			if(globalScreen.focusedElement != "NONE" )
			{
				if(e.charCode <= 366 && e.charCode >= 48)
					globalScreen.focusedElement.text += globalEventObject.lchar;
				else if(e.charCode == 32) //spacebar pressed add a space
					globalScreen.focusedElement.text += " ";
				else if(e.keyCode == 8) //backspace pressed delete the last char code
					globalScreen.focusedElement.text = globalScreen.focusedElement.text.substr(0,globalScreen.focusedElement.text.length-1);
			}
		}
		else
		{
			globalEventObject.mouseX = e.pageX - globalEventObject.screen.x;
			globalEventObject.mouseY = e.pageY - globalEventObject.screen.y;
		}
		
		
		
		//assign pressed char
		globalEventObject.ltype = e.type;
		globalEventObject.print();
		globalEventObject.sendEvent(e); //send this event to subitems of screen
	}
	
	this.print = function()
	{
		var info = "event info:<br /> mouse X = "+this.mouseX;
		info 	+= "<br /> mouse Y = "+this.mouseY;
		info 	+= "<br /> last input = "+globalEventObject.lchar;
		info 	+= "<br /> last char code = "+globalEventObject.lccode;
		info 	+= "<br /> event type = "+this.ltype;
		document.getElementById("info").innerHTML = info;
	}
	
	/*send event triggers to items of screen object if they have a corresponding trigger set*/
	this.sendEvent = function(e)
	{
		//first we search this.screen.sections to find on which section that event was occured
		var si = Math.floor(this.mouseX/this.screen.swidth);
		var sj = Math.floor(this.mouseY/this.screen.sheight);
		
		
		if(this.screen.sections[si] == undefined)
		{
		//alert("mouse X / screen.swidth = "+this.mouseX +" / "+this.screen.swidth+"  =  "+ (this.mouseX/this.screen.swidth))
		}
		
		//now we narrowed our look positions to a this.screen.sections[si][sj]
		for(var i in this.screen.sections[si][sj].innerObjects)
		{
			var temp = this.screen.sections[si][sj].innerObjects[i];
			if( this.mouseX >= temp.position.x && this.mouseX <= temp.position.x+temp.size.width  )
			if( this.mouseY >= temp.position.y && this.mouseY <= temp.position.y+temp.size.height )
			{
				// we got our object lets check for event handler
				for(var j in temp["on"+e.type])
				{
					temp["on"+e.type][j](e,temp); //run corresponding event trigger
				}
			}
			
		}
	}
	
}
globalEventObject = new events();

